/*
 * Copyright (c) 2008, Xiong Yingfei, the University of Tokyo
 * All rights reserved.
 */
package ac.jp.u_tokyo.SyncLib.language2;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import ac.jp.u_tokyo.SyncLib.language.Const;
import ac.jp.u_tokyo.SyncLib.language.GraphCombinatorBody;
import ac.jp.u_tokyo.SyncLib.language2.SetMemberExpr.*;

public class Helper
{

	protected static List<GraphCombinatorBody.Var> higherVar2LowerVar(Collection<Var> var)
	{
		List<GraphCombinatorBody.Var> result = new LinkedList<GraphCombinatorBody.Var>();
		for(Var v : var) {
			GraphCombinatorBody.Var v1 = new GraphCombinatorBody.Var(v.getLine(), v.getName());
			result.add(v1);
		}
		return result;
	}

	public static VarNameGenerator VARGENERATOR = new VarNameGenerator();
	
	public static Var createNewVar(GraphCombinatorBody body)
	{
		Var rightVar;
		rightVar = new Var(Helper.VARGENERATOR.getNextName(), 0);
		body.addOtherVar(new GraphCombinatorBody.Var(rightVar.getLine(), rightVar.getName()));
		return rightVar;
	}


	
	static SyncConstruct constructExpr(int line, Part left, VarExpr rightExpr, String rightRef, Const rightConst, ExprType exprType) {
		switch (exprType) {
			case EQUAL:
			{
				Part right;
				if (rightExpr != null) {
					right = new VarExprPart(rightExpr, false);
				}
				else if (rightRef != null) {
					right = new EqualExpr.EqualConstVarRefPart(rightRef, line);
				}
				else {
					assert rightConst != null;
					right = new EqualExpr.EqualConstPart(rightConst);
				}
				return new EqualExpr(line, left, right);
			}
			case NOTEQUAL:
			{
				RightPart right;
				if (rightExpr != null) {
					right = new NotEqualRightPart(line, new VarExprPart(rightExpr, false));
				}
				else if (rightRef != null) {
					right = new NotEqualConstValueRefRightPart(line, rightRef);
				}
				else {
					assert rightConst != null;
					right = new NotEqualConstRightPart(line, rightConst);
				}
				return new SetMemberExpr(line, left, right);
			}
			default:
			{
				assert exprType == ExprType.TESTEQUAL;
				RightPart right;
				if (rightExpr != null) {
					right = new TestEqualRightPart(line, new VarExprPart(rightExpr, false));
				}
				else if (rightRef != null) {
					right = new TestEqualConstValueRefRightPart(line, rightRef);
				}
				else {
					assert rightConst != null;
					right = new TestEqualConstRightPart(line, rightConst);
				}
				return new SetMemberExpr(line, left, right);
				
			}
		}
		
		
	}
}

enum ExprType {
	EQUAL,
	NOTEQUAL,
	TESTEQUAL
}
